package homework.newenergycharging.WorkALL.Mapper;

import homework.newenergycharging.WorkALL.CombinedWritable;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WorkALL_Mapper extends Mapper<LongWritable, Text, Text, CombinedWritable> {
    private final Text outKey = new Text();               // 输出键
    private final CombinedWritable outValue = new CombinedWritable(); // 输出值

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 输入数据示例：1366563,7.78,0.0,2014-11-18 15:40:26,2014-11-18 17:11:04,15,17,1.510555556,Tue,android,35897499,582873,461655,0,3,0,1,0,0,0,0,0
        String[] keys = value.toString().split(",");

        // 设置 outKey 为星期几（keys[8]）
        outKey.set(keys[8]);

        // 设置计数（固定为 1，对应 Work01_Mapper 的逻辑）
        outValue.setCount(new LongWritable(1));

        double value1 = Double.parseDouble(keys[7]);
        outValue.setValue1(new DoubleWritable(value1));

        double value2 = Double.parseDouble(keys[1]);
        outValue.setValue2(new DoubleWritable(value2));

        // 输出键值对
        context.write(outKey, outValue);
    }
}